﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Achievements;
using PH.Logic.Achievements;
using PH.Logic.Systems;
using PH.Model.Systems;
namespace PH.Web.Management.Areas.Services.Controllers
{
    [RouteArea("Services")]
    [RoutePrefix("ByArchive")]
    public class ByArchiveApiController : ServiceBase
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<ArchiveModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                base.Authorize();
                var user = base.GetUserByToken(request);

                ArchiveQuery sea = new ArchiveQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.AType = request.Value<byte>("aType");
                sea.Expired = request.Value<bool?>("expired");
                sea.BureauID = user.AssociatedBureauID == null ? 0 : user.AssociatedBureauID.Value;
                sea.SchoolID = user.AssociatedSchoolID == null ? 0 : user.AssociatedSchoolID.Value;
                sea.Key = request.Value<string>("key");
                var result = ArchiveLogic.Gets(sea);
                return new JsonEntitySet<ArchiveModel>(result);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ArchiveModel>(null, errinfo, false);
        }
    }
}
